package com.lagou.dao.impl;


import java.util.ArrayList;
import java.util.List;

import javax.sql.rowset.CachedRowSet;


import com.lagou.dao.UserDao;
import com.lagou.entity.User;
import com.lagou.util.JDBCUtil2;

public class UserDaoImpl implements UserDao{
	JDBCUtil2 util=JDBCUtil2.getInit();
	@Override
	public User login(String email, String pwd) {
		
		User u=null;
			
			
			CachedRowSet rs=util.query("select * from t_user where u_email=? and u_pwd=?", email,pwd);
			try{
				if(rs.next()){
					u=new User();
					u.setUid(rs.getInt("u_id"));
					u.setUemail(rs.getString("u_email"));
					u.setUpwd(rs.getString("u_pwd"));
					u.setUstatus(rs.getInt("u_status"));
					
				}
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				util.closeCacheRowSet(rs);
			}
			return u;
	}

	@Override
	public boolean add(User u) {
		boolean flag=util.update("insert into t_user (u_email,u_pwd) values(?,?)", u.getUemail(),u.getUpwd());
		if(flag) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public boolean queryByEmail(User u) {
		String sql="SELECT *  from t_user where u_email=?";
		CachedRowSet rs=util.query(sql,u.getUemail());
		boolean flag=false;
		try {
			if(rs.next()) {
				
				flag=true;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			util.closeCacheRowSet(rs);
		}
		return flag;
	}

	@Override
	public boolean updateByEmail(User u) {
		boolean flag=util.update("UPDATE t_user SET u_pwd =? WHERE u_email=? ", u.getUpwd(),u.getUemail());
		if(flag) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public User queryById(int id) {
		String sql="SELECT *  from t_user where u_id=?";
		CachedRowSet rs=util.query(sql,id);
		User u=new User();
		
		try {
			if(rs.next()) {
			u.setUemail(rs.getString("u_email"));	
				u.setUid(rs.getInt("u_id"));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			util.closeCacheRowSet(rs);
		}
		return u;
	}

	@Override
	public boolean updatebpwdByemail(String email, String pwd) {
		// TODO Auto-generated method stub
		return util.update("UPDATE t_user SET u_pwd =? WHERE u_email=? ", pwd,email);
	}
	
}
